package com.sstir.doctor.result;

import com.sstir.doctor.common.BusinessResponseEnum;
import com.sstir.doctor.config.Contants;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RespResult<T>{

    /**
     * 返回码
     */
    private int code;
    /**
     * 返回消息
     */
    private String msg;

    private T data;

    private long time;


    public RespResult(T data) {
        this.data = data;
    }

    public RespResult(T data, String msg) {
        super();
        this.data = data;
        this.msg = msg;
    }

    public RespResult(Throwable e) {
        super();
        this.msg = e.getMessage();
        this.code = 500;
    }

    public static <T> RespResult<T> ok() {
        RespResult<T> qr = new RespResult<T>();
        qr.code = Contants.SUCCESS_CODE;
        qr.msg = Contants.SUCCESS_MSG;
        qr.time = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return qr;
    }

    public static <T> RespResult<T> ok(T data) {
        RespResult<T> qr = new RespResult<T>();
        qr.code = Contants.SUCCESS_CODE;
        qr.msg = Contants.SUCCESS_MSG;
        qr.time = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        qr.data =  data;
        return qr;
    }

    public static <T> RespResult<T> badRequest() {
        RespResult<T> rj = new RespResult<T>();
        rj.code = Contants.FAIL_CODE;
        rj.msg = Contants.FAIL_MSG;
        rj.time = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return rj;
    }

    public static <T> RespResult<T> badRequest(BusinessResponseEnum businessResponseEnum) {
        RespResult<T> rj = new RespResult<T>();
        rj.code = businessResponseEnum.getCode();
        rj.msg = businessResponseEnum.getMessage();
        rj.time = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return rj;
    }

    public static <T> RespResult<T> badRequest(Integer code, String message) {
        RespResult<T> rj = new RespResult<T>();
        rj.code = code;
        rj.msg = message;
        rj.time = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return rj;
    }

    public static <T> RespResult<T> badRequest(Integer code, String message, T data) {
        RespResult<T> rj = new RespResult<T>();
        rj.code = code;
        rj.data = data;
        rj.msg = message;
        rj.time = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return rj;
    }


    public boolean isOk(){
        return Contants.SUCCESS_CODE == this.code;
    }
}
